
$(function () {
    var DetailApp = angular.module('DetailApp',['NetworkRequest'])
    DetailApp.controller("DetailController",["$scope","$http", "$location", function ($scope, $http, $location) {
     //判断类型 "?type=atricle&articleId=10".indexOf("article")
    // console.log($scope.url,$scope.router)
        //获取文章或者话题的ID
        var Id = (targetPageId(location.search));
        console.log(Id)
        if(location.search.indexOf("article") !== -1){
            console.log("这是文章详情")
            //文章类型
            $scope.rootTitle = "文章详情"
            // 请求数据
            $scope.getArticleDetail(
                Id,
                function (res) {
                if(res.status == 200 && res){

                    showContent(res.data.article,res.data.comments)

                }else if(res.status == 403 ){
                    //登录失败
                    alert("你还没有登录")
                    window.location.href = $scope.router.tologin

                }else if(res.status == 404){
                    //没有找到页面
                    alert("出错啦！！！")
                    //跳转的是相对路径
                    window.location.href = $scope.router.returnArticle
                }
            },function (error) {
                console.log(error)
            })


            // 删除文章按钮
            $scope.delete = ()=>{
                var articleId = Id
                console.log(articleId)
                //确认删除按钮
                var del =  window.confirm("您确定要删除吗");
                if(del == true){
                    $scope.deleteArticle(
                        articleId,
                        function (res) {
                           if(res.status === 200 && res){
                              alert("删除成功")
                               window.history.go(-1)
                           }
                    })
                }else {
                    return
                }
            }

        }
        else if(location.search.indexOf("topic") !== -1){
            console.log("这是话题详情")

            //话题类型
            $scope.rootTitle = "话题详情"
            // 请求数据
            $scope.getTopicDetail(
                Id,
                function (res) {
                   if(res.status == 200 && res){

                    showContent(res.data.topic,res.data.comments)
                }
                   else if(res.status == 403 ){
                    //登录失败
                       alert("你还没有登录")
                       window.location.href = $scope.router.tologin
                }
                   else if(res.status == 404){
                    //没有找到页面
                       alert("出错啦！！！")
                       window.location.href = $scope.router.returnTopic
                }
                 },
                function (error) {
                console.log(error)
             })



            // 删除话题按钮
            $scope.delete = ()=>{
                var topicId = Id
                //确认删除按钮
                var del =  window.confirm("您确定要删除吗");
                if(del == true){
                    $scope.deleteTopic(
                        topicId,
                        function (res) {
                            if(res.status == 200 && res){
                                alert("删除成功")
                                window.history.go(-1)
                            }

                        })
                    // $http.get("/api/v1/admin/article/delete").success()
                }else {
                    return
                }
            }
        }

        // 显示内容详情
        function showContent(type,comments){
            // console.log("内容："+ type.content)
            $('.content').html( type.content)
             // $scope.content = type.content
            $scope.title = type.title
            console.log("评论是"+comments)
             $scope.comments = comments
        }



        // 显示/隐藏输入框点击
        $scope.replyInput = function(_this){
            // _this.show = false
            // console.log(_this.show)
            _this.show =  !_this.show
            // console.log(_this.show )
        }
        //回复按钮点击
        $scope.replyBtn = function($event,_this){
            console.log($event.currentTarget.previousElementSibling.value)
            // console.log($("commentContent").val())
            // 获取回复的评论
            var commentContent = $event.currentTarget.previousElementSibling.value
            // 判断是否已经显示输入框
            if(_this.show == true && commentContent !== ''){
                //获取回复用户ID
                var replyUserId = _this.commentUserId
                // 获取所在文章/话题/视频ID
                // var articleId = $scope.articleId


                console.log("回复的内容：" + commentContent, "回复用户ID:"+ replyUserId, "文章/话题/视频ID:" +　Id)
                // 回复评论
                $scope.reply(commentContent, replyUserId, Id,function (res) {
                    if(res.status == 200 && res){
                        alert("回复成功!")
                        $scope.replyInput(_this)
                        // 刷新页面
                        location.reload()
                    }else{
                        alert("请稍候尝试！")
                    }
                },function (error) {
                    console.log(error)
                })

                // alert("提交成功")
                // $scope.replyInput(_this)


                // $http.get('/api/v1/admin/comment/reply').success(function(res){
                //     if(res.status  == 200 && res){
                //         alert("回复成功!")
                //         $scope.replyInput(_this)
                //     }else{
                //         alert("回复失败，请稍后重试！")
                //     }
                // })
            }else if(_this.show == true && $('.comments input').val() == ''){
                alert("请先填写回复!")
            }else if(_this.show == undefined){
                alert("请先填写回复!")
                $scope.replyInput(_this)
            }
        }


    }])

    //获取文章或者话题的ID
    function  targetPageId(search) {
        var searchURL = search;
        console.log(searchURL)
        searchURL = searchURL.substring(1, searchURL.length);
        var targetPageId = searchURL.split("&")[1].split("=")[1];
        return targetPageId
    }

})